package cn.stylefeng.guns.modular.system.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.system.entity.monthTotalindexEntity;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

/**
 * 查询 TMS面板 数据
 */
@Controller
@RequestMapping("/panelTms")
public class panelTmsController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private OrderTransportService orderTransportService;

    @Autowired
    private TDispatchService tDispatchService;

    @Autowired
    private BasicOperateService basicOperateService;
    
    /**
     * 查询当日总指标
     */
    @ResponseBody
    @RequestMapping("/dayTotalindex")
    public ResponseData dayTotalindex(OrderTransportParam orderTransportParam) {
    	User user = userService.getcurrentUser();
    	orderTransportParam.setDeliverId(user.getDeliverId());
    	orderTransportParam.setCreateTime(new Date());
    	Integer dayNewOrder = orderTransportService.sumDayNewOrderCount(orderTransportParam);//日新订单量
    	
    	OrderTransportParam orderTransportParam2 = new OrderTransportParam();
    	orderTransportParam2.setDeliverId(user.getDeliverId());
    	Integer DayWeiFaYunOrder = orderTransportService.sumDayWeiFaYunOrderCount(orderTransportParam2);//累计未发订单量
    	
    	OrderTransportParam orderTransportParam3 = new OrderTransportParam();
    	orderTransportParam3.setDeliverId(user.getDeliverId());
    	orderTransportParam3.setCreateTime(new Date());
    	Double DayOrderVolume = orderTransportService.sumDayOrderVolume(orderTransportParam3);//日订单总方数
    	
    	TDispatchParam tDispatchParam = new TDispatchParam();
    	tDispatchParam.setDeliverId(user.getDeliverId());
    	tDispatchParam.setMoney(DateUtil.getDate());
    	Integer DayFaYunCar = tDispatchService.sumDayFaYunCarCount(tDispatchParam);//日发运总车次
    	
    	cn.stylefeng.guns.modular.system.entity.dayTotalindex dayTotalindex = new cn.stylefeng.guns.modular.system.entity.dayTotalindex();
    	dayTotalindex.setDayNewOrder(dayNewOrder.toString());
    	dayTotalindex.setDayWeiFaYunOrder(DayWeiFaYunOrder.toString());
    	if(DayOrderVolume != null) {
    		dayTotalindex.setDayOrderVolume(DayOrderVolume.toString());
    	} else {
    		dayTotalindex.setDayOrderVolume("0");
    	}
    	dayTotalindex.setDayFaYunCar(DayFaYunCar.toString());
    	
        return ResponseData.success(dayTotalindex);
    }
    
    /**
     * 查询当月总指标
     */
    @ResponseBody
    @RequestMapping("/monthTotalindex")
    public ResponseData monthTotalindex(OrderTransportParam orderTransportParam) {
    	User user = userService.getcurrentUser();
    	orderTransportParam.setDeliverId(user.getDeliverId());
    	orderTransportParam.setCreateTime(new Date());
    	Integer monthNewOrder = orderTransportService.sumMonthNewOrderCount(orderTransportParam);//月新订单量
    	
    	OrderTransportParam orderTransportParam2 = new OrderTransportParam();
    	orderTransportParam2.setDeliverId(user.getDeliverId());
    	orderTransportParam2.setCreateTime(new Date());
    	Integer monthYiqianshouOrder = orderTransportService.sumMonthYqianshouOrderCount(orderTransportParam2);//已签收订单量
    	
    	OrderTransportParam orderTransportParam3 = new OrderTransportParam();
    	orderTransportParam3.setDeliverId(user.getDeliverId());
    	orderTransportParam3.setCreateTime(new Date());
    	Double monthOrderVolume = orderTransportService.sumMonthOrderVolume(orderTransportParam3);//日订单总方数
    	
    	TDispatchParam tDispatchParam = new TDispatchParam();
    	tDispatchParam.setDeliverId(user.getDeliverId());
    	tDispatchParam.setMoney(DateUtil.getMonth());
    	Integer monthFaYunCar = tDispatchService.sumMonthFaYunCarCount(tDispatchParam);//日发运总车次
    	
    	monthTotalindexEntity monthTotalindex = new monthTotalindexEntity();
    	monthTotalindex.setMonthNewOrder(monthNewOrder.toString());
    	monthTotalindex.setMonthYiqianshouOrder(monthYiqianshouOrder.toString());
    	if(monthOrderVolume != null) {
    		monthTotalindex.setMonthOrderVolume(monthOrderVolume.toString());
    	} else {
    		monthTotalindex.setMonthOrderVolume("0");
    	}
    	monthTotalindex.setMonthFaYunCar(monthFaYunCar.toString());
    	
        return ResponseData.success(monthTotalindex);
    }
    
    /**
     * 查询各项目当日订单总量
     */
    @ResponseBody
    @RequestMapping("/dayTotalOrderCount")
    public ResponseData dayTotalOrderCount(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setCreateTime(new Date());
        return ResponseData.success(basicOperateService.dayTotalOrderCount(basicOperateParam));
    }
    
    /**
     * 查询 各项目当月与上月订单总量对比
     */
    @ResponseBody
    @RequestMapping("/monthTotalOrderCount")
    public ResponseData monthTotalOrderCount(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateProvinces(sdf.format(DateUtil.getLastDate(date)));
		basicOperateParam.setOperateCiywhere(sdf.format(date));
		List<BasicOperateResult> list = basicOperateService.monthTotalOrderCount(basicOperateParam);
		for (BasicOperateResult r : list) {
			r.setOperateProvinces(sdf.format(DateUtil.getLastDate(date)));
			r.setOperateCiywhere(sdf.format(date));
			break;
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询各项目当日方数总量
     */
    @ResponseBody
    @RequestMapping("/dayTotalOrderVolume")
    public ResponseData dayTotalOrderVolume(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setCreateTime(new Date());
        return ResponseData.success(basicOperateService.dayTotalOrderVolume(basicOperateParam));
    }
    
    /**
     * 查询 各项目当月与上月方数总量对比
     */
    @ResponseBody
    @RequestMapping("/monthTotalOrderVolume")
    public ResponseData monthTotalOrderVolume(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateProvinces(sdf.format(DateUtil.getLastDate(date)));
		basicOperateParam.setOperateCiywhere(sdf.format(date));
		List<BasicOperateResult> list = basicOperateService.monthTotalOrderVolume(basicOperateParam);
		for (BasicOperateResult r : list) {
			r.setOperateProvinces(sdf.format(DateUtil.getLastDate(date)));
			r.setOperateCiywhere(sdf.format(date));
			break;
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询 各项目订单发运进度分析
     */
    @ResponseBody
    @RequestMapping("/dayTotalFayun")
    public ResponseData dayTotalFayun(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setOperateProvinces(DateUtil.getDate());
        return ResponseData.success(basicOperateService.dayTotalOrderFayunCount(basicOperateParam));
    }
    
    /**
     * 查询 各项目未发运订单量分析
     */
    @ResponseBody
    @RequestMapping("/dayTotalFayunFenxi")
    public ResponseData dayTotalFayunFenxi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setOperateProvinces(DateUtil.getDateS());
        return ResponseData.success(basicOperateService.dayTotalFayunFenxi(basicOperateParam));
    }
    
    /**
     * 查询 各项目未送达订单量分析
     */
    @ResponseBody
    @RequestMapping("/dayTotalWeisongdaFenxi")
    public ResponseData dayTotalWeisongdaFenxi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setOperateProvinces(DateUtil.getDateS());
        return ResponseData.success(basicOperateService.dayTotalWeisongdaFenxi(basicOperateParam));
    }
    
    /**
     * 查询 各项目未签收订单量分析
     */
    @ResponseBody
    @RequestMapping("/dayTotalWeiqianshouFenxi")
    public ResponseData dayTotalWeiqianshouFenxi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
    	basicOperateParam.setOperateProvinces(DateUtil.getDateS());
        return ResponseData.success(basicOperateService.dayTotalWeiqianshouFenxi(basicOperateParam));
    }
    
    /**
     * 查询 
     * 月度订单环节量分析 == 待调、装车、在途、送达、签收
     */
    @ResponseBody
    @RequestMapping("/monthTotalHuanjieFenxi")
    public ResponseData monthTotalHuanjieFenxi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateBankaccount(sdf.format(date));
        return ResponseData.success(basicOperateService.monthTotalHuanjieFenxi(basicOperateParam));
    }
    
    /**
     * 查询 
     * 月各项目发运订单 时间段占比
     */
    @ResponseBody
    @RequestMapping("/monthFayunShixiaoZhanbi")
    public ResponseData monthFayunShixiaoZhanbi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateCiywhere(sdf.format(date));
		List<BasicOperateResult> list = basicOperateService.monthTotalFayunOrderCount(basicOperateParam);//查询  各项目当月  总发运订单数
		for (BasicOperateResult r : list) {
			BigDecimal sum = new BigDecimal(r.getOperateTel());//发运总单量
			BigDecimal as = new BigDecimal(0);
			if(sum.compareTo(as) == 1) {
				BasicOperateParam basicOperateParam2 = new BasicOperateParam();
				basicOperateParam2.setDeliverId(user.getDeliverId());
				basicOperateParam2.setOperateCiywhere(sdf.format(date));
				basicOperateParam2.setId(r.getId());
				BasicOperateResult result = basicOperateService.monthTotalFayunOrderCountById(basicOperateParam2);//查询  项目当月  发运订单时间段数
				BigDecimal operateTel = new BigDecimal(result.getOperateTel());
				if(operateTel.compareTo(as) == 1) {
					operateTel = operateTel.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//12h内发运 占比
					result.setOperateTel(operateTel.toString());
				}
				BigDecimal operateEmall = new BigDecimal(result.getOperateEmall());
				if(operateEmall.compareTo(as) == 1) {
					operateEmall = operateEmall.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//24h内发运 占比
					result.setOperateEmall(operateEmall.toString());
				}
				BigDecimal operateProvinces = new BigDecimal(result.getOperateProvinces());
				if(operateProvinces.compareTo(as) == 1) {
					operateProvinces = operateProvinces.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//48h内发运 占比
					result.setOperateProvinces(operateProvinces.toString());
				}
				r.setResult(result);
			}
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询 
     * 月各项目送达订单 时间段占比
     */
    @ResponseBody
    @RequestMapping("/monthSongdaShixiaoZhanbi")
    public ResponseData monthSongdaShixiaoZhanbi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateCiywhere(sdf.format(date));
		List<BasicOperateResult> list = basicOperateService.monthTotalSongdaOrderCount(basicOperateParam);//查询  各项目当月  总送达订单数
		for (BasicOperateResult r : list) {
			BigDecimal sum = new BigDecimal(r.getOperateTel());//送达总单量
			BigDecimal as = new BigDecimal(0);
			if(sum.compareTo(as) == 1) {
				BasicOperateParam basicOperateParam2 = new BasicOperateParam();
				basicOperateParam2.setDeliverId(user.getDeliverId());
				basicOperateParam2.setOperateCiywhere(sdf.format(date));
				basicOperateParam2.setId(r.getId());
				BasicOperateResult result = basicOperateService.monthTotalSongdaOrderCountById(basicOperateParam2);//查询  项目当月  送达订单时间段数
				BigDecimal operateTel = new BigDecimal(result.getOperateTel());
				if(operateTel.compareTo(as) == 1) {
					operateTel = operateTel.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//12h内送达 占比
					result.setOperateTel(operateTel.toString());
				}
				BigDecimal operateEmall = new BigDecimal(result.getOperateEmall());
				if(operateEmall.compareTo(as) == 1) {
					operateEmall = operateEmall.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//24h内送达 占比
					result.setOperateEmall(operateEmall.toString());
				}
				BigDecimal operateProvinces = new BigDecimal(result.getOperateProvinces());
				if(operateProvinces.compareTo(as) == 1) {
					operateProvinces = operateProvinces.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//48h内送达 占比
					result.setOperateProvinces(operateProvinces.toString());
				}
				r.setResult(result);
			}
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询 
     * 月各项目签收订单 时间段占比
     */
    @ResponseBody
    @RequestMapping("/monthQianshouShixiaoZhanbi")
    public ResponseData monthQianshouShixiaoZhanbi(BasicOperateParam basicOperateParam) {
    	User user = userService.getcurrentUser();
    	basicOperateParam.setDeliverId(user.getDeliverId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		basicOperateParam.setOperateCiywhere(sdf.format(date));
		List<BasicOperateResult> list = basicOperateService.monthTotalQianshouOrderCount(basicOperateParam);//查询  各项目当月  总签收订单数
		for (BasicOperateResult r : list) {
			BigDecimal sum = new BigDecimal(r.getOperateTel());//送达总单量
			BigDecimal as = new BigDecimal(0);
			if(sum.compareTo(as) == 1) {
				BasicOperateParam basicOperateParam2 = new BasicOperateParam();
				basicOperateParam2.setDeliverId(user.getDeliverId());
				basicOperateParam2.setOperateCiywhere(sdf.format(date));
				basicOperateParam2.setId(r.getId());
				BasicOperateResult result = basicOperateService.monthTotalQianshouOrderCountById(basicOperateParam2);//查询  项目当月  签收订单时间段数
				BigDecimal operateTel = new BigDecimal(result.getOperateTel());
				if(operateTel.compareTo(as) == 1) {
					operateTel = operateTel.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//12h内签收 占比
					result.setOperateTel(operateTel.toString());
				}
				BigDecimal operateEmall = new BigDecimal(result.getOperateEmall());
				if(operateEmall.compareTo(as) == 1) {
					operateEmall = operateEmall.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//24h内签收 占比
					result.setOperateEmall(operateEmall.toString());
				}
				BigDecimal operateProvinces = new BigDecimal(result.getOperateProvinces());
				if(operateProvinces.compareTo(as) == 1) {
					operateProvinces = operateProvinces.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//48h内签收 占比
					result.setOperateProvinces(operateProvinces.toString());
				}
				r.setResult(result);
			}
		}
        return ResponseData.success(list);
    }
    

}
